Fix oversized Mainline hook context injection - #203
Merged
Conversation
Mainline fork PR importStatus: Mainline fork PR import failed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Prevent oversized Mainline hook context from exhausting model prompts
Summary
The shared renderer change also protects Claude Code, Codex, and Cursor. On the same large repository fixture, all four agents now receive approximately 4 KiB of session-start context instead of approximately 6.7 MB.
Validation
go test ./...go vet ./...go test -race ./internal/hooks/...git diff --checkopencadhook-size verification:Review notes
The core incident is fixed for every agent using the shared renderer. The Pi extension additionally provides host-side defense in depth. Follow-up hardening opportunities identified during review—avoiding full in-memory serialization before compaction, adding a common host-output boundary for other agents, and bounding Pi stderr—are non-blocking and outside this fix's scope.
Authoritative Mainline intent record
Mainline Intent
Intent:
int_5a162dfcStatus:
proposedTitle: 限制 Pi hook 上下文注入体积
What changed
将 session-start 的完整 status/sync JSON 改为保留 active intent、authority、计数和下一步命令的紧凑摘要;对大型 turns、uncovered、proposals、suggestions、actionable items 与任意 sync 数组设置条数/字符串限制和 omitted 计数;在通用 renderer 增加 32KiB/16KiB 硬预算,并在 Pi extension 增加 stdout、单段 context、合并 context 三层防御性上限。新增大型 fixture,验证预算、关键信息保留和省略正文不泄露。
Why
Pi 会把 hook 返回的 systemPromptAppend 拼入模型 system prompt。大型 active intent 曾让 session-start 返回约 6.7MB 文本,使空白新会话也超过 1M token 上下文。需要默认按需加载详情,并在 renderer 与宿主 extension 两端建立确定性安全边界。
Decisions
Subsystems: hooks dispatcher context renderer, Pi coding-agent extension, hook context regression tests